﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PwManager
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {

            if (ValidateText() && ValidateAccountAndPwd(txtPwd.Text))
            {
                this.Hide();
                new Form1(this).Show();
            }
        }

        bool ValidateText()
        {
            if (string.IsNullOrWhiteSpace(txtPwd.Text))
            {
                lbMsgForPwd.Text = "*请输入密码";
                return false;
            }
            else
                lbMsgForPwd.Text = "";
            return true;
        }

        bool ValidateAccountAndPwd(string pwd)
        {
            var sql = "select pwd from strongbox where id=1";
            var pwdInDb = SQLiteHelper.ExecuteScalar(sql, null);
            if (pwdInDb == null)
            {
                lbMsgForPwd.Text = "*尚未注册";
                return false;
            }
            var pwdInPut = EncryptHelper.MD5_Encrypt(pwd);
            if (pwdInDb.Equals(pwdInPut))
                return true;
            lbMsgForPwd.Text = "*密码错误";
            return false;
        }

        private void lkbRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var sql = "select account from strongbox where id=1";
            var account = SQLiteHelper.ExecuteScalar(sql, null);

            new Register(Convert.ToString(account)).Show();
        }
    }
}
